package com.song.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.song.service.PaymentFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @Author Klys
 * @Date 2022/6/15 10:08
 * @Version 1.0
 */
@RestController
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")  //全局降级
public class FeignController {

    @Value("${server.port}")
    private String port;

    @Autowired
    private PaymentFeignService paymentFeignService;

    @GetMapping(value = "/consumer/payment/test")
    public String test(){
        return paymentFeignService.test()+"我是comsumer controller！!!";
    }

    //开启客户端自己的服务降级功能
    @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="1500")
    })

    @GetMapping(value = "/consumer/payment/timeout")
    public String timeout(){
//        openfeign默认等待一秒钟，超过后会报错。即调用后得不到响应时间是在一秒内。
        return paymentFeignService.timeout();
    }

    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="1500")
    })
//    @HystrixCommand //对客户端实现解耦，全局配置服务降级
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id)
    {
        //模拟问题  ----除数不能为零
        int age = 10/0;
        String result = paymentFeignService.timeout();
        return result;
    }
    public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id)
    {
        return "我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o";
    }

    //客户端解耦
    // 下面是全局fallback方法
    public String payment_Global_FallbackMethod()
    {
        return "Global异常处理信息，请稍后再试，/(ㄒoㄒ)/~~";
    }
}
